#include <iostream>

//通过模板类实现链表的简单尾插入与链表的输出，顺便复习指针与引用的知识
using namespace std;

//节点的定义
template <class T>
struct node{
public:
    T element;
    node<T> *next;

    //初始构造函数以便生成头节点和插入的实现
    node(){
        this->element=0;
        this->next=NULL;
    }
    node(T &element,node<T> *next){
        this->element=element;
        this->next=next;
    }
};

//链表的定义
template<class T>
class chain: public node<T>
{
public:
    node<T> *firstnode;
    int listsize;

public:
    chain(){
        firstnode=new node<T>();
        firstnode->next=NULL;
        listsize=0;
    }

    chain(const chain<T> &thelist){
        listsize=thelist.listsize;
        node<T> *index_node=thelist.firstnode;
        index_node=thelist.firstnode;
        firstnode=new node<T> (index_node->element);
        index_node=index_node->next;
        node<T> *creatnode=firstnode;
        while(index_node!=NULL){
            creatnode->next =new node<T>(index_node->element);
            creatnode=creatnode->next;
            index_node=index_node->next;
        }
        creatnode->next =NULL;
    }

    ~chain(){
        while(firstnode != NULL){
            node<T> *nextnode=firstnode->next;
            delete firstnode;
            firstnode=nextnode;
        }
    }

    T& get(int theindex){
        node<T> *currentnode =firstnode;
        for (int i=0;i<theindex;i++){
            currentnode= currentnode-> next;
            return currentnode->element;
        }
    }
    
    // FIXME: `erase` is better, `index` is better
    void erased(int theindex){
        node<T> *deletenode;
        if(theindex==0){
            deletenode=firstnode;
            firstnode=firstnode->next;
        }
        else{
            node<T> *p=firstnode;
            for (int i=0;i<theindex-1;i++){
                p=p->next;
                deletenode=p->next;
                p->next=p->next->next;
            }
            listsize--;
            delete deletenode;
        }
    }
    
    void insertd(int theindex,T& theElement){
        node<T> *p=firstnode;
        for(int i=0;i<theindex-1;i++){
            p=p->next;
        }
        p->next=new node<T>(theElement,p->next);
        listsize++;
    }
};

int main()
{  
    chain<int> n1;
    for (int i = 0; i < 10; i++)
	{
		n1.insertd(i + 1, i);
	}
	
    node<int> *p=n1.firstnode;
    for(int i=0;i<n1.listsize-1;i++){
        p=p->next;
        cout<<p->element<<endl;
    }

    return 0;
}

